<template>
  <div class="logincode">
    <el-form label-position="rigth">
      <el-form-item>
        <el-input
          placeholder="手机号"
          prefix-icon="el-icon-mobile-phone"
          v-model="formLabelAlign.phone"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          auto-complete="new-password"
          placeholder="验证码"
          class="code"
          autocomplete="off"
          prefix-icon="el-icon-lock"
          v-model="formLabelAlign.code"
        ></el-input>
        <el-form-item>
          <el-button
            style="width: 160px"
            class="codebtn btn"
            :disabled="isDisabled"
            type="primary"
            @click="sendCode"
            >{{ buttonText }}</el-button
          >
        </el-form-item>
      </el-form-item>
    </el-form>
    <div class="error" v-show="errmessage.err">
      <i class="el-icon-warning"></i>
      <span>{{ errmessage.message }}</span>
    </div>
    <el-button class="loginbtn" type="primary" @click="submit">登录</el-button>
    <div class="register" @click="goRegister()">立即注册</div>
  </div>
</template>

<script>
import { mapGetters, mapActions } from "vuex";

export default {
  data() {
    return {
      buttonText: "获取验证码",
      isDisabled: false, // 是否禁止点击发送验证码按钮
      canSubmit: false,
      errmessage: {
        err: false,
        message: "手机号错误，请重输入",
      },
      formLabelAlign: {
        phone: "",
        code: "",
      },
    };
  },
  methods: {
    ...mapActions(["sendSms", "codeLogin"]),
    checkPhone() {
      if (!this.formLabelAlign.phone) {
        this.errmessage = {
          err: true,
          message: "请输入手机号",
        };
        this.canSubmit = false;
      } else if (!/^1[3|4|5|7|8|9][0-9]\d{8}$/.test(this.formLabelAlign.phone)) {
        this.errmessage = {
          err: true,
          message: "手机号错误，请重输入",
        };
        this.canSubmit = false;
      } else {
        this.errmessage = {
          err: false,
          message: "",
        };
        this.canSubmit = true;
      }
      return this.canSubmit;
    },
    checkCode() {
      if (!this.formLabelAlign.code) {
        this.errmessage = {
          err: true,
          message: "请输入验证码",
        };
        this.canSubmit = false;
      } else{
        this.canSubmit = true;
      }
      return this.canSubmit;
    },
    submit() {
      if (this.checkCode()&&this.checkPhone()) {
        // 提交修改
        let data = {
          phone: this.formLabelAlign.phone,
          phone_code: this.formLabelAlign.code,
        };
        this.codeLogin(data).then((res) => {
          this.$message({
            message: "恭喜你，登录成功",
            type: "success",
          });
          this.$router.push({
            name: "index",
          });
        });
      }
    },
    // 发送验证码
    sendCode() {
      if (this.checkPhone()) {
        this.changeBtnText();
        this.sendSms({ mobile: this.formLabelAlign.phone,type:3 }).then((res) => {
          this.changeBtnText();
        });
      }
    },
    changeBtnText() {
      let time = 60;
      this.isDisabled = true;
      this.buttonText = "已发送（" + time + " s）";
      let timer = setInterval(() => {
        time--;
        this.buttonText = "已发送（" + time + " s）";
        if (time === 0) {
          clearInterval(timer);
          this.buttonText = "重新获取";
          this.isDisabled = false;
        }
      }, 1000);
    },
    goRegister() {
      this.$router.push({
        name: "register",
      });
    },
  },
};
</script>

<style scoped>
.el-form {
  margin-top: 35px;
  font-size: 16px;
}
.el-form-item {
  margin: 0;
}
.el-form-item__content .el-input /deep/ .el-input__icon {
  font-size: 18px;
  line-height: 30px;
  margin: 0 20px 40px 20px;
  position: absolute;
  top: -14px;
}
.el-form-item__content .el-input /deep/ .el-input__inner {
  height: 60px;
  margin: 0 0 30px 0;
  font-size: 16px;
  font-family: Adobe Heiti Std;
  font-weight: normal;
  color: #333333;
  padding-left: 60px;
}
.password-setting {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 15px 0 17px 0;
}
.el-checkbox[data-v-14dddfbe] {
  line-height: 0;
}
.el-checkbox {
  width: 57px;
  height: 13px;
  font-size: 14px;
  font-family: Source Han Sans CN;
  font-weight: 400;
  color: #333333;
}
.forget {
  cursor: pointer;
}
.error {
  width: 460px;
  height: 28px;
  background: #feefef;
  border: 1px dashed #ffd5d5;
  font-size: 12px;
  font-family: Source Han Sans CN;
  font-weight: 400;
  color: #ff1e1e;
  line-height: 28px;
  position: relative;
}
.error i {
  font-size: 14px;
  margin: 0 10px;
}
.loginbtn {
  width: 460px;
  height: 60px;
  background: #0B4696;
  box-shadow: 0px 7px 27px 0px rgba(50, 129, 255, 0.51);
  border-radius: 2px;
  margin-top: 17px;
  font-size: 18px;
  font-family: Source Han Sans CN;
  font-weight: 400;
  color: #ffffff;
  line-height: 15px;
  letter-spacing: 4px;
}
.register {
  width: 57px;
  height: 13px;
  font-size: 14px;
  font-family: Source Han Sans CN;
  font-weight: 400;
  color: #0e93e4;
  line-height: 70px;
  float: right;
  cursor: pointer;
}
.code {
  width: 280px;
  padding-top: 0;
  margin-bottom: 10px;
}
.btn {
  margin-left: 20px;
  width: 160px;
  height: 60px;
  font-size: 16px;
  font-family: Source Han Sans CN;
  font-weight: 400;
  text-align: center;
  /* line-height: 60px; */
  cursor: pointer;
}
.codebtn {
  background: #f1f7ff;
  color: #0B4696;
  border: none;
}
.passbtn {
  background: #f2f2f2;
  color: #999999;
}
.el-form-item /deep/ .el-form-item__content {
  display: flex;
}
.open {
  width: 60px;
  height: 60px;
  position: absolute;
  top: 15px;
  right: 0px;
  cursor: pointer;
}
</style>